/* 文件上传处理
 * UploadFileReducer.js
 */

import { combineReducers } from 'redux';


function imageList(state=Immutable.List(),action){
	switch (action.type) {
		case 'ADD_IMAGE':
			let imagelist=state;
			action.data.forEach(item=>{
				imagelist=imagelist.insert(0,Immutable.Map(item));
			});
			let newList=Immutable.List(),i=0;
			imagelist.forEach(item=>{
				if(i<10) newList=newList.push(item);
				i++;
			});
			return newList;
		case 'LIST_IMAGE':
			return Immutable.fromJS(action.datalist);
		case 'IMAGE_LIST_NEXT_PAGE':
			return Immutable.fromJS(action.datalist);
		case 'IMAGE_LIST_PRE_PAGE':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

function imageCatetory(state=Immutable.List(),action){
	switch (action.type) {
		case 'IMAGE_CATEGORY_LIST':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

function musicList(state=Immutable.List(),action){
	switch (action.type) {
		case 'ADD_MUSIC':
			let imagelist=state;
			action.data.forEach(item=>{
				imagelist=imagelist.insert(0,Immutable.Map(item));
			});
			let newList=Immutable.List(),i=0;
			imagelist.forEach(item=>{
				if(i<10) newList=newList.push(item);
				i++;
			});
			return newList;
		case 'LIST_MUSIC':
			return Immutable.fromJS(action.datalist);
		case 'MUSIC_LIST_NEXT_PAGE':
			return Immutable.fromJS(action.datalist);
		case 'MUSIC_LIST_PRE_PAGE':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

function musicCatetory(state=Immutable.List(),action){
	switch (action.type) {
		case 'MUSIC_CATEGORY_LIST':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

//用于分页使用，存储的是每一页的最大id的数组集合
function pageIdArr(state=Immutable.List(),action){
	switch (action.type) {
		case 'UPLOAD_LIST_NEXT_PAGE':
			return state.push(action.maxId);
		case 'UPLOAD_LIST_PRE_PAGE':
			return state.pop();
		case 'UPLOAD_LIST_PAGE_CLEAR':
			return Immutable.List();
		default:
		   	return state;
  	}
}

function fileList(state=Immutable.List(),action){
	switch (action.type) {
		case 'ADD_FILE':
			let imagelist=state;
			action.data.forEach(item=>{
				imagelist=imagelist.insert(0,Immutable.Map(item));
			});
			let newList=Immutable.List(),i=0;
			imagelist.forEach(item=>{
				if(i<10) newList=newList.push(item);
				i++;
			});
			return newList;
		case 'LIST_FILE':
			return Immutable.fromJS(action.datalist);
		case 'FILE_LIST_NEXT_PAGE':
			return Immutable.fromJS(action.datalist);
		case 'FILE_LIST_PRE_PAGE':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

function fileCatetory(state=Immutable.List(),action){
	switch (action.type) {
		case 'FILE_CATEGORY_LIST':
			return Immutable.fromJS(action.datalist);
		default:
		   	return state;
  	}
}

export default combineReducers({imageList,imageCatetory,musicList,musicCatetory,pageIdArr,fileList,fileCatetory});
